package jjn.round1;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Objects;

/**
 * @author Jjn
 * @since 2022-10-09 15:33
 */
public class LeetCode856_ScoreOfParentheses {
    public int scoreOfParentheses(String s) {
        Deque<Integer> deque = new LinkedList<>();
        deque.push(0);
        for (Character c : s.toCharArray()) {
            if (Objects.equals(c, '(')) {
                deque.push(0);
            } else {
                int res = deque.pop();
                int top = deque.pop() + Math.max(2 * res, 1);
                deque.push(top);
            }
        }
        return deque.pop();
    }
}
